/*
 * @名称 compare
 * @功能 车款对比功能
 * @author Gina
 */
define(function(require,exports,module){
    var url = require('mod-url'),util = require('lm-util'),$ = require('$');
    require('easyui');
    require('easyui-css');
    require('easyui-icon-css');

    function setExpire(minutes){
        var date = new Date();
        date.setTime(date.getTime() + (minutes * 60 * 1000));
        return date;
    }

    function Compare(){
        /*初始化对比列表*/
        var hasCompare = util.cookie("autoCompareId")!=null&&util.cookie("autoCompareId")!="",
            autoType = util.cookie("autoCompareType"),
            autoIds;
        if(hasCompare){
            autoIds = util.cookie("autoCompareId").split(",");
            $.getJSON(url.auto+'?callback=?',{"action":"autoCompare", "autoIds":autoIds, "autoType": autoType },
                function (data) {
                    for(var p in data){
                        var html = "<li><a title=\""+ data[p].name+"\" data-modelId='"+ data[p].id+"' data-modelType='"+ data[p].type+"'>"+ data[p].name+"</a><a class=\"delete\" title=\"删除\">x</a></li>"
                        var $carName = $(html);
                        $carName.appendTo("ul#car-com-list");
                        $("#noCompare").hide();
                        $("#car-compare").show();
                    }
                });
        }else{
            $("#noCompare").show();
        }
    }
    module.exports = Compare;

    //删除对比项
    Compare.prototype.removeCompare = function(modelId){
        var that = this;
        var curCarCompare =  util.cookie("autoCompareId") == null ? [] : util.cookie("autoCompareId").split(",");
        curCarCompare.splice($.inArray(modelId,curCarCompare),1);
        if(curCarCompare.length == 0){
            util.cookie("autoCompareId", "", { expires: setExpire(60) });
            util.cookie("autoCompareType", "", { expires: setExpire(60) });
            $("#noCompare").show();
            $("a.com-btn").hide();
        }else{
            util.cookie("autoCompareId",curCarCompare,{ expires: setExpire(60) });
        }
    }
    //执行对比
    Compare.prototype.run = function(){
        var that = this;
        //清空对比项
        $("a#clearAllModel").click(function(){
            util.cookie("autoCompareId", "", { expires: setExpire(60) });
            util.cookie("autoCompareType", "", { expires: setExpire(60) });
            $("ul#car-com-list").empty();
            $("p#noCompare").show();
            $("a.com-btn").hide();
        });
        //隐藏对比框
        $("a#com-close").click(function(){
            $("div#car-compare").hide();
        });

        //添加对比
        $("button.js-compare-btn").each(function(){
            $(this).click(function(){
                $("div#car-compare").show();
                $("a.com-btn").show();
                $("p#noCompare").hide();

                var modelType = $(this).attr("data-modelType"),//车款类型
                    modelId = $(this).attr("data-modelId"),//车款ID
                    fullName = $(this).attr("data-fullName"),//车款全名
                    curCarType = util.cookie("autoCompareType") == ""||util.cookie("autoCompareType")==null ?  "" : util.cookie("autoCompareType"),//比较列表中的车款类型
                    curModelIds = util.cookie("autoCompareId") == ""||util.cookie("autoCompareId") == null ? [] : (util.cookie("autoCompareId")).split(",");//当前比较列表中的车款Id

                if(curModelIds.length < 4){
                    if( curCarType !=="" && modelType !== curCarType){
                        $("#dialog").find('p').text("类型不同不能添加到对比列表中");
                        $( "#dialog").dialog({closed:true});
                        $( "#dialog" ).dialog('open');
                    }else if($.inArray(modelId, curModelIds)>=0){
                        $("#dialog").find('p').html("对比列表中已经存在该车款");
                        $( "#dialog").dialog({closed:true});
                        $( "#dialog").dialog('open');
                    }else{
                        if(curModelIds.length == 0){
                            util.cookie("autoCompareType",modelType,{ expires: setExpire(60) });
                        }
                        curModelIds.push(modelId);
                        util.cookie("autoCompareId", curModelIds, { expires: setExpire(60) });
                        var html = "<li><a title=\""+fullName+"\" data-modelId='"+modelId+"' data-modelType='"+modelType+"'>"+fullName+"</a><a class=\"delete\" title=\"删除\">x</a></li>"
                        var $carName = $(html);
                        $carName.appendTo("ul#car-com-list");
                    }
                }else{
                    $("#dialog").find('p').text("对比车款最多不能超过4个");
                    $( "#dialog").dialog({closed:true});
                    $( "#dialog" ).dialog('open');
                }
            });
        });

        //删除一个对比项
        $("ul#car-com-list").on("click","a.delete",function(){
            var modelId = $(this).siblings("a:first").attr("data-modelId");
            $(this).parent("li").remove();
            that.removeCompare(modelId);
        });

        //开始对比
        $("#J_GoCompare").click(function(){
            var compareNum = $("ul#car-com-list>li").length;
            if(compareNum<=1){
                $("#dialog").find('p').text("对比车款不能少于2个！");
                $( "#dialog").dialog({closed:true});
                $( "#dialog" ).dialog('open');
                return false;
            }
        });
    }
});